{$MODE OBJFPC}

uses
  Windows, SysUtils, S2DMain, S2DTypes, S2DSWCanvas;

const SP_AMOUNT = 1000;

type
  TSpark = record
    X, Y, SX, SY, Age, Aging: Real;
  end;

var Sparks: array[0..SP_AMOUNT] of TSpark;
    Action,
    Back,
    Light,
    Spark: TBuffer;
    M,
    I    : Integer;

begin
  // nap resource
  LoadBMP(Back,  'Images\background.bmp');
  LoadBMP(Light, 'Images\light.bmp');
  LoadBMP(Spark, 'Images\spark.bmp');
  Randomize;
  for I:= 0 to SP_AMOUNT do
    with Sparks[I] do
    begin
      X:= Random(630-20);
      Y:= Random(38)+(439 div 2-38);
      SX:=(random(21)-10) / 5;
      SY:=(random(21)-10) / 5;
      Age:=random(100)/100;
      Aging:=(random(10)+3)/100;
    end;
  // khoi tao window
  InitWindow(640, 480);
  ShowCursor(FALSE);
  // vong lap cho toi khi an phim ESC
  repeat
    // ve background vao screen
    SwapBuffer(Screen, Back);
    // ve light vao screen
    AddBuffer(Screen, Light, 0, 160);
    // spark handle
    for I:= 0 to SP_AMOUNT do
      with Sparks[I] do
      begin
        Age:= Age+Aging*(FrameTime/20);
        X:= X+SX*(FrameTime/20);
        SY:= SY+(1/10);
        Y:= Y+SY*(FrameTime/20);
        if (Age>=1) or (X<0) or (X+20>640) or (Y<0) or (Y+20>490) then
        begin
          X:= Random(640-20);
          Y:= 439 div 2-38+Random(38);
          SX:= (Random(21)-10) / 5;
          SY:= (Random(21)-10) / 5;
          Age:= 0;
          Aging:= (Random(10)+1)/50;
        end;
        // ve spark
        AddBuffer(Screen, Spark, Round(X), Round(Y), 20*Round(Age*4), 0, 20, 20);
      end;

    // shows FPS & memory usage ra man hinh
    SysText(Screen, 10, 10, 'FPS        : '+IntToStr(S2DInfo.FPS), $FFFFFF);
    SysText(Screen, 10, 20, 'Memory used: '+IntToStr(S2DInfo.TotalAllocMemory)+' bytes', $FFFFFF);
    GetMessages;
  until NOT S2DInfo.Running OR KeyMap[VK_ESCAPE];
  // giai phong resource & thoat
  FreeBuffer(Spark);
  FreeBuffer(Light);
  FreeBuffer(Back);
  CloseWindow;
end.
